Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Ruff config #1049

Merged
merged 27 commits into from
Apr 25, 2024
Merged

Ruff config #1049

merged 27 commits into from
Apr 25, 2024

Conversation

c0rydoras
Copy link
Contributor

No description provided.

@c0rydoras c0rydoras requested a review from a team as a code owner April 16, 2024 13:03
@c0rydoras c0rydoras force-pushed the ruff-config branch 4 times, most recently from de7d763 to 16105aa Compare April 16, 2024 13:24
timed/employment/views.py Outdated Show resolved Hide resolved
@c0rydoras c0rydoras force-pushed the ruff-config branch 2 times, most recently from ef1270c to 3d61393 Compare April 16, 2024 14:54
ruff.toml Outdated Show resolved Hide resolved
Copy link
Member

@winged winged left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An interesting experiment, enabling all these checks! I think however that some of them are maybe a bit too eager, and don't really add anything of value. Not every rule available is actually useful.

I've added some comments where I think the rules go too far. Consider reverting the affected changes (Note I've only commented maybe once or twice per type of change, not on every change affected by the rule.

Also, and most importantly: Please extend your commit messages and comments a bit, so future readers know why something was done - not just oneliners.

ruff.toml Outdated Show resolved Hide resolved
timed/authentication.py Show resolved Hide resolved
timed/conftest.py Outdated Show resolved Hide resolved
timed/employment/admin.py Show resolved Hide resolved
timed/employment/serializers.py Outdated Show resolved Hide resolved
timed/reports/filters.py Outdated Show resolved Hide resolved
timed/reports/filters.py Show resolved Hide resolved
timed/reports/views.py Show resolved Hide resolved
timed/settings.py Outdated Show resolved Hide resolved
timed/employment/admin.py Outdated Show resolved Hide resolved
@c0rydoras
Copy link
Contributor Author

I'm not sure how to handle

Warning

DJ001 Avoid using null=True on string-based fields such as CharField

It would require a migration (if the null=True) gets dropped and might break something (don't know for sure tho)

@hairmare
Copy link
Contributor

hairmare commented Apr 17, 2024

I'm not sure how to handle

Warning

DJ001 Avoid using null=True on string-based fields such as CharField

It would require a migration (if the null=True) gets dropped and might break something (don't know for sure tho)

I think i agree with DJ001 per se as well as with your reasoning why this might need more consideration.

Let's deactivate this one for now and defer it to future us with a new issue.

edit: link for TODO comment: adfinis/timed#23

timed/reports/views.py Outdated Show resolved Hide resolved
@winged
Copy link
Member

winged commented Apr 18, 2024

I'm not sure how to handle
Warning
DJ001 Avoid using null=True on string-based fields such as CharField
It would require a migration (if the null=True) gets dropped and might break something (don't know for sure tho)

I think i agree with DJ001 per se as well as with your reasoning why this might need more consideration.

Let's deactivate this one for now and defer it to future us with a new issue.

edit: link for TODO comment: adfinis/timed#23

That's DB design dogma however. I do agree with it in the general case, but I think that check just goes too far. As you noted, we can do it in a separate PR sometimes, but just for the linter's sake, it's not the right thing to do

@c0rydoras
Copy link
Contributor Author

timed/subscription/admin.py:27:38: S324 Probable use of insecure hash functions in hashlib: md5
|
25 | password = self.cleaned_data.get("password")
26 | if password is not None:
27 | self.instance.password = hashlib.md5(password.encode()).hexdigest()
| ^^^^^^^^^^^ S324
28 | return super().save(commit=commit)
|

class CustomerPassword(models.Model):
    """Password per customer used for login into SySupport portal.

    Password are only hashed with md5. This model will be obsolete
    once customer center will go live.
    """

Can this be dropped, respectively ignored and dropped in a separate PR?

Since https://github.com/adfinis/customer-center does exist

c0rydoras added a commit to c0rydoras/timed-backend that referenced this pull request Apr 18, 2024
@hairmare
Copy link
Contributor

timed/subscription/admin.py:27:38: S324 Probable use of insecure hash functions in hashlib: md5 | 25 | password = self.cleaned_data.get("password") 26 | if password is not None: 27 | self.instance.password = hashlib.md5(password.encode()).hexdigest() | ^^^^^^^^^^^ S324 28 | return super().save(commit=commit) |

class CustomerPassword(models.Model):
    """Password per customer used for login into SySupport portal.

    Password are only hashed with md5. This model will be obsolete
    once customer center will go live.
    """

Can this be dropped, respectively ignored and dropped in a separate PR?

Since https://github.com/adfinis/customer-center does exist

I'm not 100% if this is still in use or not. It looks like it was part of the old SySupport portal and might have been replaced with a proper solution when the portal was migrated to being an Ember based frontend.

If this is in fact the case then we should create an issue to track the codes removal. Maybe @winged or @trowik know if it's still needed?

c0rydoras added a commit to c0rydoras/timed-backend that referenced this pull request Apr 18, 2024
@trowik
Copy link
Member

trowik commented Apr 18, 2024

timed/subscription/admin.py:27:38: S324 Probable use of insecure hash functions in hashlib: md5 | 25 | password = self.cleaned_data.get("password") 26 | if password is not None: 27 | self.instance.password = hashlib.md5(password.encode()).hexdigest() | ^^^^^^^^^^^ S324 28 | return super().save(commit=commit) |

class CustomerPassword(models.Model):
    """Password per customer used for login into SySupport portal.

    Password are only hashed with md5. This model will be obsolete
    once customer center will go live.
    """

Can this be dropped, respectively ignored and dropped in a separate PR?
Since https://github.com/adfinis/customer-center does exist

I'm not 100% if this is still in use or not. It looks like it was part of the old SySupport portal and might have been replaced with a proper solution when the portal was migrated to being an Ember based frontend.

If this is in fact the case then we should create an issue to track the codes removal. Maybe @winged or @trowik know if it's still needed?

I'm also not 100% sure, but I'd say it's safe to remove it in another PR.

@c0rydoras c0rydoras mentioned this pull request Apr 18, 2024
Copy link
Contributor

@hairmare hairmare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this is shaping up nicely and we should proceed with merging it while deferring further improvements to later PRs.

As it stands, this already activates many rules that will help keep the code base clean going forward as well as stomping several potential bugs.

c0rydoras and others added 16 commits April 23, 2024 14:54
tests: move unused fixtures to `usefixtures`
conftest: ignore ARG001 for db fixture
tracking: use named arguments for boolean (rejected)
serializers: added docstrings to files
serializers: ClassVar[dict[str, str]] annotation for included_serializers
views: make ordering tuple if its not already a tuple
move __str__'s from sphinx-doc to type annotations
move worktime_per_day and similar properties/lazy_attributes from sphinx-doc to annotations
serializers: move validate, clean from sphinx-doc to type annotations
@winged
Copy link
Member

winged commented Apr 23, 2024

#1049 (comment)

@winged this only works with

from __future__ import annotations

See PEP 563

TIL!

But I think that future thingy is more about forward declarations, not about not having to import stuff that you can import. Do weird things only if you MUST, we shouldn't blindly follow dogma

while editing code to make ruff happy about unused variables i ended up replacing unused args/kwargs with *args, **kwargs, this commit reverts this to use the argname with an underscore wherever possible
@hairmare
Copy link
Contributor

TIL!

Um, same :)

I promised @c0rydoras that i wold do some light catching up on PEPs wrt to typing, specifically wrt to the previously unknown to me string form, and i discovered a rabbit hole of PEPs that i have yet to go down fully having previously focused on getting started with typing and not deep typing.

I still have some reading to do, while i'm not (yet?) convinced that the string for is pythonic, i do think TYPE_CHECKING has value beyond just being dogma. tbh, i only figured this out when using it.

Basically, the import section has long been a part of my code that i do inspect, but i rarely write it on my own. either my IDE just add the required imports or i write them in any (isort violating) order and ruff fixes whatever i am too lazy to consider at the time of writing. Given this hand-off approach to handling imports, the TYPE_CHECKING block seems to help me have a deeper understanding of the code i write.

I've run into situations where i refactor the code and a dependency was "ruffed" to move my import into the block, this helps demonstrate that i was successful at reducing the coupling between the component under refactor and what i was working on.

Anyways.... @c0rydoras proposed to today that he would love to merge this in the near term and followup on typing discussions in #1054 and I kinda agree with him.

I think this discussion is highly valuable and don't consider it bike-shedding at all. In my opinion we can start safely using some typing without the resulting code being highly un-pythonic. I created #1054 because i see considerable value where typing can help us get rid of several low-level incorrectness's in our code base.

For this PR to get merged ASAP, which i am highly favorable of since it seems to be correct and not introduce and defects i think we have two options:

To no surprise i'm in favour of the latter option. The plan is to tackle #1054 right once this is landed.

We should also prepare the typing discussion so we can find consensus/input with a broader audience of stake holders to any policy here.

langer rede kurzer sinn, 🥺 👉 👈

does this address your concerns?

@hairmare hairmare requested a review from winged April 23, 2024 18:58
@c0rydoras c0rydoras enabled auto-merge (rebase) April 23, 2024 19:01
@c0rydoras c0rydoras disabled auto-merge April 23, 2024 19:01
@winged
Copy link
Member

winged commented Apr 24, 2024

I absolutely agree with you @hairmare - The thing I'm most scared about with this strict Ruff config is that we stop questioning the rules in cases where they don't make sense, and apply patterns just to satisfy the linter.

We've seen this a few times in this PR (not blaming @c0rydoras at all): It's often difficult to see if a linter complaint is a valid criticism or something that should be acceptable in this particular situation.

So in a weird sense having stricter linting solves a problem but introduces a new one: While earlier you had to rely on experience/knowledge to figure out whether a code structure/pattern/whatever is "good", you now have to rely on experience/knowledge/common-sense to figure out whether a linter complaint is "good". Not sure which one is preferrable. For example the whole tuple-vs-list discussion we had here: I think tuples are harder to read and easier to make mistakes with, for the (slight) benefit of them being readonly (but still replaceable)

Regarding type hints - I think it's a glorious idea, and it makes my life (via IDE/LSP/tooling) much better, but: Python will never be a strictly, fully-typed language, so I'm quite happy to make the move rather gradual, and omit it where it's just cluttering up the code without real benefit.

So, in conclusion:

  • More, stricter linters are good, but do not replace common sense / experience.
  • Type hints are good, but don't be dogmatic about it

I'm OK with merging this PR soon-ish - I'll have a final look over it and approve if I don't see any more issues

Copy link
Member

@winged winged left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! There's still a few code smells, but they're either from before your change (and so should not be addressed here) or can be fixed / cleaned up in a follow-up PR.

I'd say you have green light to merge this from me

@@ -12,13 +12,10 @@


class TimedOIDCAuthenticationBackend(OIDCAuthenticationBackend):
def get_introspection(self, access_token, id_token, payload):
def get_introspection(self, access_token, _id_token, _payload):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note (no change required): This naming pattern really only works when the method is called with positional args. We'll have to be careful to check the call sites in such cases - especially if it's implementing a protocol / defined API or overriding a base class' implementation

qs = qs[: int(value)]

return qs
return qs[: int(value)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole filter is kind of smelly - should be cleaned up in a separate PR:

  • It's not a filter only
  • It does implicit ordering
  • It does implicit "pagination"

To make it work properly, this will need corresponding fixes in the frontend as well I think

@c0rydoras
Copy link
Contributor Author

@winged how would you merge this?

I'd be in favour of squashing it and then using the fix scope

@winged
Copy link
Member

winged commented Apr 25, 2024

@winged how would you merge this?

I'd be in favour of squashing it and then using the fix scope

You have my approval, and @hairmare's as well - go ahead 👍

@c0rydoras c0rydoras merged commit 0d3c023 into adfinis:main Apr 25, 2024
3 checks passed
@c0rydoras c0rydoras deleted the ruff-config branch April 25, 2024 08:29
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants